package com.framework.camera;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.EditText;


public class MyAsyncTask extends AsyncTask<String, Integer, Long> implements OnCancelListener{

	private static final int SERVERPORT = 15680;
	private static final String SERVER_IP = "172.16.1.28";

	  final String TAG = "MyAsyncTask";
	  ProgressDialog dialog;
	  Context context;
	  
	  public MyAsyncTask(Context context){
	    this.context = context;
	  }
	  
	  @Override
	  protected void onPreExecute() {
	    Log.d(TAG, "onPreExecute");
	    dialog = new ProgressDialog(context);
	    dialog.setTitle("Please wait");
	    dialog.setMessage("Loading data...");
	    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	    dialog.setCancelable(true);
	    dialog.setOnCancelListener(this);
	    dialog.setMax(100);
	    dialog.setProgress(0);
	    dialog.show();
	  }

	  @Override
	  protected Long doInBackground(String... params) {
	    Log.d(TAG, "doInBackground - " + params[0] + params[1]);
	    
	    NetworkControl netctrl = new NetworkControl(SERVER_IP, SERVERPORT, null, null);

	    List<String> cam_list = netctrl.get_cameralist();

	    byte[] snapshot = netctrl.get_snapshot(cam_list.get(0));
	    

      BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
      bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
      Bitmap bmp = BitmapFactory.decodeByteArray(snapshot, 0, snapshot.length, bitmapFatoryOptions);
	      
	    
        String filePath = Environment.getExternalStorageDirectory() + "/test.jpg";
        File file = new File(filePath);
        file.getParentFile().mkdir();
 
        FileOutputStream fos = null;
        try {
			fos = new FileOutputStream(file);
            bmp.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
		} catch (FileNotFoundException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
        } catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} finally{
        	if(fos != null){
        		try {
					fos.close();
				} catch (IOException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
        	}
        }

	    
//	    publishProgress(100);
	    
//	    try {
//	      for(int i=0; i<10; i++){
//	        if(isCancelled()){
//	          Log.d(TAG, "Cancelled!");
//	          break;
//	        }
//	        Thread.sleep(1000);
//	        publishProgress((i+1) * 10);
//	      }
//	    } catch (InterruptedException e) {
//	      Log.d(TAG, "InterruptedException in doInBackground");
//	    }
	    
	    
	    return 123L;
	  }
	  
	  @Override
	  protected void onProgressUpdate(Integer... values) {
	    Log.d(TAG, "onProgressUpdate - " + values[0]);
//	    dialog.setProgress(values[0]);
	  }
	  
	  @Override
	  protected void onCancelled() {
	    Log.d(TAG, "onCancelled");
	    dialog.dismiss();
	  }

	  @Override
	  protected void onPostExecute(Long result) {
	    Log.d(TAG, "onPostExecute - " + result);
	    dialog.dismiss();
	  }

	  @Override
	  public void onCancel(DialogInterface dialog) {
	    Log.d(TAG, "Dialog onCancell... calling cancel(true)");
	    this.cancel(true);
	  }
}
